﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using FrankJie.IDAL;
using FrankJie.Model;

namespace FrankJie.SQLServerDAL
{
	/// <summary>
	/// 友情链接数据访问类
	/// </summary>
	public partial class Link:ILink
	{
        /// <summary>
        /// 增加一个友情链接
        /// </summary>
        /// <param name="linkInfo">友情链接实体</param>
        /// <returns>返回友情链接ID</returns>
        public int Add(LinkInfo linkInfo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("INSERT INTO tblLink(");
			strSql.Append("LinkName,Url,Rss,Description,Visible,UpdateTime,Rating,MId)");
			strSql.Append(" VALUES (");
			strSql.Append("@LinkName,@Url,@Rss,@Description,@Visible,@UpdateTime,@Rating,@MId)");
			strSql.Append(";SELECT @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@LinkName", SqlDbType.VarChar,50),
					new SqlParameter("@Url", SqlDbType.VarChar,200),
					new SqlParameter("@Rss", SqlDbType.VarChar,200),
					new SqlParameter("@Description", SqlDbType.VarChar,50),
					new SqlParameter("@Visible", SqlDbType.Bit,1),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@Rating", SqlDbType.Int,4),
					new SqlParameter("@MId", SqlDbType.Int,4)};
            parameters[0].Value = linkInfo.LinkName;
            parameters[1].Value = linkInfo.Url;
            parameters[2].Value = linkInfo.Rss;
            parameters[3].Value = linkInfo.Description;
            parameters[4].Value = linkInfo.Visible;
            parameters[5].Value = linkInfo.UpdateTime;
            parameters[6].Value = linkInfo.Rating;
            parameters[7].Value = linkInfo.MId;

			object obj = SQLHelper.GetSingle(SQLHelper.FrankJieDBConnStr,CommandType.Text,strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

        /// <summary>
        /// 更新一个友情链接
        /// </summary>
        /// <param name="linkInfo">友情链接实体</param>
        /// <returns>返回是否成功</returns>
        public bool Update(LinkInfo linkInfo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("UPDATE tblLink SET ");
            strSql.Append("LinkName=@LinkName,Url=@Url,Rss=@Rss,Description=@Description,Visible=@Visible,UpdateTime=@UpdateTime,Rating=@Rating,MId=@MId");
			strSql.Append(" WHERE Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@LinkName", SqlDbType.VarChar,50),
					new SqlParameter("@Url", SqlDbType.VarChar,200),
					new SqlParameter("@Rss", SqlDbType.VarChar,200),
					new SqlParameter("@Description", SqlDbType.VarChar,50),
					new SqlParameter("@Visible", SqlDbType.Bit,1),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@Rating", SqlDbType.Int,4),
					new SqlParameter("@MId", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = linkInfo.LinkName;
            parameters[1].Value = linkInfo.Url;
            parameters[2].Value = linkInfo.Rss;
            parameters[3].Value = linkInfo.Description;
            parameters[4].Value = linkInfo.Visible;
            parameters[5].Value = linkInfo.UpdateTime;
            parameters[6].Value = linkInfo.Rating;
            parameters[7].Value = linkInfo.MId;
            parameters[8].Value = linkInfo.Id;

			int rows=SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr,CommandType.Text,strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 删除一个友情链接
        /// </summary>
        /// <param name="id">友情链接ID</param>
        /// <returns>返回是否成功</returns>
		public bool Delete(int id)
		{
            string strSql = "DELETE FROM tblLink WHERE Id=@Id";
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        /// <summary>
        /// 获取友情链接列表
        /// </summary>
        /// <returns>返回友情链接集合</returns>
        public List<LinkInfo> GetList()
        {
            string strSql = "SELECT * FROM tblLink";
            List<LinkInfo> linkInfoList = new List<LinkInfo>();
            LinkInfo linkInfo = new LinkInfo();
            using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql))
            {
                linkInfo = new LinkInfo();
                linkInfo.Id = int.Parse(dr["Id"].ToString());
                linkInfo.LinkName = dr["LinkName"].ToString();
                linkInfo.Url = dr["Url"].ToString();
                linkInfo.Rss = dr["Rss"].ToString();
                linkInfo.Description = dr["Description"].ToString();
                if ((dr["Visible"].ToString() == "1") || (dr["Visible"].ToString().ToLower() == "true"))
                {
                    linkInfo.Visible = true;
                }
                else
                {
                    linkInfo.Visible = false;
                }
                linkInfo.UpdateTime = DateTime.Parse(dr["UpdateTime"].ToString());
                linkInfo.Rating = int.Parse(dr["Rating"].ToString());
                linkInfo.MId = int.Parse(dr["MId"].ToString());
                linkInfoList.Add(linkInfo);
            }
            return linkInfoList;
        }
	}
}

